/*@@

    Copyright (c) 2011 BlueSky LTD.  All rights reserved. 

    Created_datetime : 2011-1-11 16:30:32

    File Name :	base_os_nix.inl

    Author : Wang Hu (ghoststears@gmail.com)

    Description : 

@@*/


#ifndef __BASE_OS_NIX_INL__
#define __BASE_OS_NIX_INL__


#include "blk_def.h"

#include "stdlib.h"
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>

namespace BLK
{
    inline
    std::size_t list_dir(const std::string& path, std::vector<std::string>& files)
    {
		char fulldir[256] = {0};
		getcwd(fulldir, 256);

		DIR* dp;
		struct dirent* entry;
		struct stat statbuf;

		if((dp = opendir(path.c_str())) == NULL)
			return 0;

		chdir(path.c_str());
		while((entry = readdir(dp)) != NULL)
		{
			lstat(entry->d_name, &statbuf);
			if(S_ISDIR(statbuf.st_mode))
				continue;
			else
				files.push_back(entry->d_name);
		}

		chdir(fulldir);
		closedir(dp);

        return files.size();
    }

    inline
    bool is_file(const std::string& path)
    {
        struct stat info;
        int rt = stat(path.c_str(), &info); 
        
        return (S_IFREG & info.st_mode);
    }

    inline
    bool is_dir(const std::string& path)
    {
        struct stat info;
        stat(path.c_str(), &info); 

        return (S_IFDIR & info.st_mode);
    }
}

#endif /* __BASE_OS_NIX_INL__ */
